//部分控制器
var app = angular.module('myApp.controllers');

//example首页
app.controller('collectionIndex', ["$scope", "utilService", "$rootScope", '$ionicScrollDelegate', function ($scope, utilService, $rootScope, $ionicScrollDelegate) {
  // tab切换 如果需要无限加载加这个 不需要就算了。
  $scope.integralParams = {
    limit: 10,
    page: -1
  }
  // 当前参数
  $scope.data = {
    id: "", //当前选中tab的ID
    top: 0,//当前选中tab滚动的位置
    isLoad: false,//当前选中tab是否是第一次load
    currPage: 0,//当前选中tab当前第几页
    hasMore: true,
    posts: [],//当前选中tab的数据
  };
  $scope.tabs = ["店铺", "服务"];
  $scope.tabMap = {
    "店铺": {
      currPage: -1,
      posts: [],
      isLoad: true,
      hasMore: true,
      id: "店铺",
      top: 0
    },
    "服务": {
      currPage: -1,
      posts: [],
      isLoad: true,
      hasMore: true,
      id: "服务",
      top: 0
    }
  };
  $scope.isActive = function(item) {
    return item == $scope.data.id;
  }

  //标签切换
  $scope.switchTab = function (id) {
    var pos = $ionicScrollDelegate.getScrollPosition();
    pos && ($scope.data.top = pos.top);

    var map = $scope.tabMap[id];
    if (!map) {
      map = $scope.tabMap[id] = {};
    }

    $scope.data = map;
    $scope.integralParams.page = map.currPage;
    $ionicScrollDelegate.resize();
    !map.top && (map.top = 0);
    $ionicScrollDelegate.scrollTo(0, map.top || 0, false);
  };
  $scope.switchTab($scope.tabs[0]); //默认选择第一个
  /*模拟数据*/
  $scope.shopList = [
    {id:0,isEdit:false},
    {id:1,isEdit:false},
    {id:2,isEdit:false}
  ];

  /*模拟数据*/
  $scope.serviceList = [
    {id:0,isEdit:false},
    {id:1,isEdit:false},
    {id:2,isEdit:false}
  ];
  // 点击去详情
  $scope.toDetail = function (index, id) {
    if ($scope.shopList[index]['isEdit']) {
      $scope.shopList[index]['isEdit'] = false;
    } else {
      //跳转至详情
      console.log(1)
      //utilService.$state.go('shop.detail');
    }
  }
  // 点击去详情
  $scope.toServiceDetail = function (index, id) {
    if ($scope.serviceList[index]['isEdit']) {
      $scope.serviceList[index]['isEdit'] = false;
    } else {
      //跳转至详情
      console.log(1)
      //utilService.$state.go('shop.detail');
    }
  }
  // 取消关注
  $scope.cancelFocus = function (id) {
    console.log('取消')
  }
  //点击去编辑
  $scope.toEdit = function (index) {
    $scope.shopList[index]['isEdit'] = true;
  }
  //点击去编辑
  $scope.toServiceEdit = function (index) {
      event.stopPropagation();
    $scope.serviceList[index]['isEdit'] = true;
  }
    $scope.$on('$ionicView.beforeEnter', function () {
        //页面刚加载
        utilService.$ionicLoading.show(HX_CONFIG.loadingBase);
    }, false);
    $scope.$on('$ionicView.afterEnter', function () {
        utilService.$rootScope.rootData.title = '我的收藏';
        //页面加载完成
        utilService.$ionicLoading.hide();
    }, false);
    $rootScope.$on('$ionicView.beforeLeave', function (e, ion) {
        //页面离开之前
        //alertPopup && alertPopup.close();

    }, false);

}]);
